#!/usr/bin/perl

package Swignition::Export::Location;
use Swignition::Export;
use Swignition::Export::Address;
use Swignition::Export::Contact;
use Swignition::Export::Point;
use HTML::Entities qw(encode_entities_numeric);

sub is
{
	my $s = shift;
	return Swignition::Export::Address::is($s)||Swignition::Export::Point::is($s);
}

sub outputs
{
	return [
		{
			code  => 'KML',
			name  => 'Keyhole Markup Language',
			type  => 'application/vnd.google-earth.kml+xml',
			multi => 1
		},
		{
			code  => 'Text',
			name  => 'Plain text',
			type  => 'text/plain'
		}
	];
}

sub multiKML
{
	my $triples = shift;
	my $rv;

	foreach my $s (keys %$triples)
	{
		$S = findSubject($triples, $s);
		next  # Skip this geo if it has an associated address.
			if (Swignition::Export::Point::is($S)
			&&  firstOf($S, 'urn:ietf:rfc:2426#adr'));
		$rv .= _outKML($triples, $s)
			if (defined $S && is($S));
	}

	return  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
			. "<!-- KML output by Swignition. -->\r\n"
			. "<kml xmlns=\"http://earth.google.com/kml/2.0\">\r\n"
			. $rv
			. "</kml>\r\n";
}

sub outText
{
	my $triples = shift;
	my $sURL    = shift;
	my $sObj    = findSubject($triples, $sURL);
	
	return $sURL
		if (!$sObj);
	
	if (Swignition::Export::Address::is($sObj))
		{ return Swignition::Export::Address::outText($triples, $sObj->uri); }

	if (Swignition::Export::Point::is($sObj))
		{ return Swignition::Export::Point::outText($triples, $sObj->uri); }
		
	if (Swignition::Export::Contact::is($sObj))
	{
		my $adr = firstOf($sObj, 'urn:ietf:rfc:2426#adr',
			'http://www.w3.org/2006/vcard/ns#adr',
			'http://www.w3.org/2006/vcard/ns#homeAdr',
			'http://www.w3.org/2006/vcard/ns#workAdr',
			'http://www.w3.org/2006/vcard/ns#unlabelledAdr',
			'http://www.w3.org/2001/vcard-rdf/3.0#ADR',
			'http://www.w3.org/2000/10/swap/pim/contact#address');
		my $Adr = findSubject($triples, $adr);
		
		if (Swignition::Export::Address::is($Adr))
			{ return Swignition::Export::Address::outText($triples, $Adr->uri); }
			
		my $geo = firstOf($sObj, 'urn:ietf:rfc:2426#geo',
			'http://www.w3.org/2006/vcard/ns#geo',
			'http://www.w3.org/2001/vcard-rdf/3.0#GEO');
		my $Geo = findSubject($triples, $geo);

		if (Swignition::Export::Point::is($Geo))
			{ return Swignition::Export::Point::outText($triples, $Geo->uri); }
	}
	
	return $sObj->uri;
}

sub outKML
{
	my $triples = shift;
	my $sURL    = shift;
	
	my $rv = _outKML($triples, $sURL);

	return  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
			. "<kml xmlns=\"http://earth.google.com/kml/2.0\">\r\n"
			. $rv
			. "</kml>\r\n"
			. "<!-- KML output by Swignition. -->\r\n";
}

sub _outKML
{
	my $triples = shift;
	my $sURL    = shift;
	my $sObj    = findSubject($triples, $sURL);
	my $rv;
	my $adr;
	
	$rv  = "\t<Placemark>\r\n";
	
	# Co-ordinates
	if (Swignition::Export::Point::is($sObj))
	{
		my $coords = Swignition::Export::Point::outText3($triples, $sObj->uri);
		$rv .= "\t\t<Point><coordinates>$coords</coordinates></Point>\r\n";
	}
	else
	{
		my $geo = firstOf($sObj, 'urn:ietf:rfc:2426#geo',
			'http://www.w3.org/2006/vcard/ns#geo',
			'http://www.w3.org/2001/vcard-rdf/3.0#GEO');
		my $Geo = findSubject($triples, $geo);
		if ($Geo)
		{
			my $coords = Swignition::Export::Point::outText3($triples, $Geo->uri);
			$rv .= "\t\t<Point><coordinates>$coords</coordinates></Point>\r\n";
		}
		elsif ($geo)
		{
			$rv .= "\t\t<Point><coordinates>$geo</coordinates></Point>\r\n";
		}
	}

	# Address
	if (Swignition::Export::Address::is($sObj))
	{
		$adr = Swignition::Export::Address::outText($triples, $sObj->uri);
		$rv .= "\t\t<address>".encode_entities_numeric($adr)."</address>\r\n";
	}
	
	# Name
	my $title = firstOf($sObj,
		'http://purl.org/dc/terms/title',
		'http://purl.org/dc/elements/1.1/title') || $adr;
	$rv .= "\t\t<name>".encode_entities_numeric($title)."</name>\r\n"
		if ($title);

	# Description
	my $desc = firstOf($sObj,
		'http://purl.org/dc/terms/description',
		'http://purl.org/dc/elements/1.1/description',
		'http://purl.org/dc/terms/abstract') || $title;
	$rv .= "\t\t<description>".encode_entities_numeric($desc)."</description>\r\n"
		if ($desc);
	
	$rv .= "\t</Placemark>\r\n";
	
	return $rv;
}

1;
